package com.eamois.service.impl;

import java.io.Serializable;

import org.courser.service.ServiceSupportImpl;
import org.courser.ui.PaginationHelper;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.eamois.dao.BaseDao;
import com.eamois.domain.BaseEntity;
import com.eamois.service.BaseService;
import com.eamois.util.Constants;

public class BaseServiceImpl<T extends BaseEntity,K extends BaseDao<T>> extends ServiceSupportImpl<T,K> implements BaseService<T>{
	@Autowired
	public PaginationHelper paginationHelper;

	public PaginationHelper getPaginationHelper() {
		return paginationHelper;
	}

	public void setPaginationHelper(PaginationHelper paginationHelper) {
		this.paginationHelper = paginationHelper;
	}

	@SuppressWarnings("unchecked")
	public DetachedCriteria createQuery(Class cls)
	{
		return this.getPaginationHelper().createQuery(cls);
	}

	public int batchRemove(java.io.Serializable[] ids)
	{
		return this.getDao().batchRemove(ids);
	}



	public int batchDraft(Serializable[] ids)
	{
		return this.getDao().batchDraft(ids);
	}

	public int  batchTrash(Serializable[] ids)
	{
		return this.getDao().batchTrash(ids);
	}

	public int batchSubmit(Serializable[] ids)
	{
		return this.getDao().batchSubmit(ids);
	}

	@Override
	public void save(T  entity)
	{
		if (entity.getSign()==null)
			entity.setSign(Constants.SIGN_FORMAL);
		super.save(entity);
	}

	public void draft(T entity)
	{
		entity.setSign(Constants.SIGN_DRAFT);
		super.save(entity);
	}

	public void trash(T entity)
	{
		entity.setSign(Constants.SIGN_TRASH);
		super.save(entity);
	}

	public void submit(T entity)
	{
		entity.setSign(Constants.SIGN_FORMAL);
		super.save(entity);
	}

	public PaginationSupport pageQueryDraft(final DetachedCriteria detachCriteria, final PaginationSupport page)
	{
		detachCriteria.add(Restrictions.eq("sign", Constants.SIGN_DRAFT));
		return super.listByPage(detachCriteria, page);
	}

	public PaginationSupport pageQueryFormal(final DetachedCriteria detachCriteria, final PaginationSupport page)
	{
		detachCriteria.add(Restrictions.eq("sign", Constants.SIGN_FORMAL));
		return super.listByPage(detachCriteria, page);
	}


	public PaginationSupport pageQueryTrash(final DetachedCriteria detachCriteria, final PaginationSupport page)
	{
		detachCriteria.add(Restrictions.eq("sign", Constants.SIGN_TRASH));
		return super.listByPage(detachCriteria, page);
	}
}
